/****************************************************************
 *Author:       zxx
 *UnityVersion：2019.4.1f1
 *Date:         2020/07/30 11:28:29
 *Description:    
*****************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoBehaviour {

    public static  ObjectPool Instance;

    private  ManagerVars vars;

    private  List<GameObject> enemyList = new List<GameObject>();

    private List<GameObject> rocketList = new List<GameObject>();

    private  int initCount = 5;

    private void Awake()
    {
        Instance = this;

        vars = ManagerVars.GetVars();

        
    }
    private void Start()
    {
        Init();
    }
    /// <summary>
    /// 初始化对象池
    /// </summary>
    private void Init()
    {
        //for (int i = 0; i < initCount; i++)
        //{
        //    InstantiateObject(vars.enemyPrefab,ref enemyList);
        //}
        for (int i = 0; i < initCount; i++)
        {
            InstantiateObject(vars.rocketPrefab, ref rocketList);
        }

    }
    /// <summary>
    /// h获取敌人
    /// </summary>
    /// <returns></returns>
    public GameObject GetEnemy()
    {
        for (int i = 0; i < enemyList.Count; i++)
        {
            if (!enemyList[i].activeInHierarchy)
            {
                return enemyList[i];
            }
        }
        return InstantiateObject(vars.enemyPrefab, ref enemyList);

    }

    /// <summary>
    /// 获取火箭
    /// </summary>
    /// <returns></returns>
    public GameObject GetRocket()
    {
        for (int i = 0; i < rocketList.Count; i++)
        {
            if (!rocketList[i].activeInHierarchy)
            {
                return rocketList[i];
            }
        }
        return InstantiateObject(vars.rocketPrefab, ref rocketList);

    }
    /// <summary>
    /// 生成物体通用方法
    /// </summary>
    /// <param name="prefab"></param>
    /// <param name="list"></param>
    /// <returns></returns>
    private GameObject InstantiateObject(GameObject prefab, ref List<GameObject> list)
    {
        GameObject go = Instantiate(prefab, transform);
        go.SetActive(false);
        list.Add(go);
        return go;
    }


}

